<template>
  <el-dialog :visible.sync="isShow" width="600" center>
    <el-form :model="add_form" :rules="rules" ref="add_form" label-width="80px">
      <el-form-item label="学科编号" prop="rid">
        <el-input v-model="add_form.rid"></el-input>
      </el-form-item>
      <el-form-item label="学科名称" prop="name">
        <el-input v-model="add_form.name"></el-input>
      </el-form-item>
      <el-form-item label="学科简称" prop="short_name">
        <el-input v-model="add_form.short_name"></el-input>
      </el-form-item>
      <el-form-item label="学科简介" prop="intro">
        <el-input v-model="add_form.intro"></el-input>
      </el-form-item>
      <el-form-item label="学科备注" prop="remark">
        <el-input v-model="add_form.remark"></el-input>
      </el-form-item>
    </el-form>

    <div slot="title" class="dialog-title">
      <span>{{ mode == "add" ? "学科新增" : "学科编辑" }}</span>
      <span class="el-icon-close icon" @click="isShow = false"></span>
    </div>
    <span slot="footer" class="dialog-footer" center>
      <el-button @click="isShow = false">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { add_subject, editSubject } from "@/api/getSubjectList.js";
export default {
  props: ["mode", "editInfo"],
  watch: {
    isShow() {
      if (this.mode == "edit") {
        this.add_form = this.editInfo;
      }
      if (!this.isShow) {
        this.add_form = {
          rid: "", //	是	string	学科编号
          name: "", //	是	string	学科名称
          short_name: "", //	否	string	学科简称
          intro: "", //	否	string	学科简介
          remark: "", //	否	string	备注
        };
        this.$refs.add_form.resetFields();
      }
    },
  },
  data() {
    return {
      isShow: false,
      add_form: {
        rid: "", //	是	string	学科编号
        name: "", //	是	string	学科名称
        short_name: "", //	否	string	学科简称
        intro: "", //	否	string	学科简介
        remark: "", //	否	string	备注
      },
      rules: {
        rid: { required: true, message: "请输入学科编号", trigger: "change" },
        name: { required: true, message: "请输入学科名称", trigger: "change" },
        short_name: {
          required: true,
          message: "请输入学科简称",
          trigger: "change",
        },
        intro: { required: true, message: "请输入学科简介", trigger: "change" },
        remark: {
          required: true,
          message: "请输入学科备注",
          trigger: "change",
        },
      },
    };
  },
  methods: {
    submit() {
      if (this.mode == "add") {
        console.log("打印add");
        this.$refs.add_form.validate((valid) => {
          if (valid) {
            add_subject(this.add_form)
              .then(() => {
                this.$message.success("添加成功");
                this.isShow = false;
                // this.getData();
                this.$emit("getlist");
              })
              .catch((err) => {
                this.$message.error(err);
              });
          } else {
            console.log("error submit!!");
            return false;
          }
        });
      } else if (this.mode == "edit") {
        this.$refs.add_form.validate((valid) => {
          if (valid) {
            editSubject(this.add_form)
              .then(() => {
                this.$message.success("编辑成功!");
                this.$emit("getlist");
                this.isShow = false;
              })
              .catch((err) => {
                this.$message.error(err);
              });
          } else {
            console.log("error submit!!");
            return false;
          }
        });
      }
    },
  },
};
</script>

<style></style>
